<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

//url validation
function isValidUrl($url)
{
	$result	= ereg("^[a-zA-Z0-9]+://[^ ]+[a-z0-9](\.[a-z]{2,4})$", $url, $trashed);
	if($result)
	//if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $url))
		return true;
	else
		return false;
}

function isValidDomain($domain)
{
	$domain = 'teSTgjhgj';
	if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9]$/i", $domain))
		return true;
	else 
		return false;
}
function isNumber($string)
{
	$result=ereg("^[0-9]+$",$string,$trashed);
	if(!($result))
		return false;
	else
		return true;
}
//function to validate email
function isValidEmail($strEmail)
{
	$result=ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$",$strEmail);
	if($result)
		return true;
	else
		return false;
}

//function to check duplicate records while insertion
function IsRecordExist4Add($table,$fieldName,$condition)
{
	$flag = false;
	$query	= "SELECT COUNT($fieldName) AS total_rows FROM $table WHERE $condition";
	//$res	= mysql_query($query);
	if($res=mysql_query($query))
	{
		$rs		= mysql_fetch_object($res);
		if($rs->total_rows>0)
			$flag = true;
	}
	return $flag;
}

function getTotRec($fieldName,$table, $condition='')
{
	if($condition !='')
		$condition = " WHERE ".$condition;
		
	$res	= mysql_query("SELECT COUNT(".$fieldName.") AS tot_rec FROM ".$table.$condition);
	if($res)
		{
			$rs		= mysql_fetch_object($res);
			if($rs->tot_rec>0)
				return $rs->tot_rec;
			else
				return 0; 
		}
	else
		return 0;
}
//get sum value of particular field
function getTotSum($fieldName,$table, $condition='')
{
	if($condition !='')
		$condition = " WHERE ".$condition;
		
	$res	= mysql_query("SELECT SUM(".$fieldName.") AS sum_value FROM ".$table.$condition);
	if($res)
		{
			$rs		= mysql_fetch_object($res);
			if($rs->tot_rec>0)
				return $rs->tot_rec;
			else
				return 0; 
		}
	else
		return 0;
}
function getRow($table, $fieldList, $condition='')
{
	if($condition !='')
		$condition = ' WHERE '.$condition;
		//echo "SELECT ".$fieldList." FROM ".$table.$condition;
	$res		= mysql_query("SELECT ".$fieldList." FROM ".$table.$condition);
	if($res)
		$rs		= mysql_fetch_array($res);
	return $rs;
}
//function to chech duplicate records while updation
function IsRecordExist4Update($Table,$FieldName,$FieldValue,$IdName,$IdValue,$UserId)
{
	$UnqQuery="select $FieldName from $Table where $IdName<>'$IdValue' and UserID='$UserId' and $FieldName='$FieldValue'";
	$UnqRes=mysql_query($UnqQuery);
	$UnqRows=mysql_num_rows($UnqRes);
	if($UnqRows>0)
		return true;
	else
		return false;
}
//function to update records
function updateRecords($table,$fieldList,$condition='')
{
	if($condition !='')
		$condition =" WHERE ".$condition;
	$query	= "UPDATE ".$table." SET ".$fieldList.$condition;
	//mysql_query($query);
	if(mysql_query($query))
		return true;
	else
		return false;
}
//function to update records
function insertRecord($table,$fieldList)
{
	$query	= "INSERT INTO ".$table." SET ".$fieldList;
	//mysql_query($query);
	if(mysql_query($query))
		return true;
	else
		return false;
}
//function to delete records from table
function deleteRecord($table,$condition)
{
	$query	= "DELETE FROM ".$table." WHERE ".$condition;
	if(mysql_query($query))
		return true;
	else
		return false;
}
function listFiles($path)
{
	$dirname 	= $path;
	$dir_list	= array();
	$dh = opendir($dirname) or die("couldn't open directory");
	
	while (!(($file = readdir($dh)) === false ) ) 
		{
			if (is_dir("$dirname/$file") && $file !='.' && $file!='..') 
				$dir_list[]	= $file;
		}
	closedir($dh);
	return $dir_list;
}
function createDir($path)
{
	//echo $path;
	mkdir($path,0777);
}

//fuction to check file exists
function isFileExists($path)
{
	//To check if the directory exists before calling mkdir(), use files_exists, e.g.
	if (file_exists($path)) 
		return true;
	else
		return false;
}
function removeFiles($dir)
{
	$current_dir = opendir($dir);
	while($entryname = readdir($current_dir))
		{
			if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!=".."))
				{
				   deldir("${dir}/${entryname}");
				}
			elseif($entryname != "." and $entryname!="..")
				{
				   unlink("${dir}/${entryname}");
				}
		}
	closedir($current_dir);
	//rmdir(${dir});
}


//function to send mail
function sendMail($content,$subject,$from,$to)
{
	
	/* message */ 
	$message = $content;
	
	/* To send HTML mail, you can set the Content-type header. */ 
	$headers  = "MIME-Version: 1.0\r\n"; 
	$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
	
	/* additional headers */ 
	$headers .= "From: $from\r\n"; 
	
	/* and now mail it */ 
	if(mail($to, $subject, $message, $headers))
		return true;
	else
		return false;
}
//function to find ratingAvg
//accept ratecount and sum of ratevalue and 
//returns the corresponding rating image
function ratingImg($count,$rateSum)
{
	//calculate rating average if content is rated,
	//atleast one time
	if($count>0)
		{
			$ratingAvg	= round(($rateSum/$count),1);
			
			//give rating image path depends on the 
			//rating average
			if($ratingAvg > 0 and $ratingAvg<1)
				if($ratingAvg >= 0.5)
					$rateImg='images/r0_half.gif';
				else
					$rateImg='images/r0.gif';
			elseif($ratingAvg >= 1 and $ratingAvg<2)
				if($ratingAvg >= 1.5)
					$rateImg='images/r1_half.gif';
				else
					$rateImg='images/r1.gif';
			elseif($ratingAvg >= 2 and $ratingAvg<3)
				if($ratingAvg >= 2.5)
					$rateImg='images/r2_half.gif';
				else
					$rateImg='images/r2.gif';
			elseif($ratingAvg >= 3 and $ratingAvg<4)
				if($ratingAvg >= 3.5)
					$rateImg='images/r3_half.gif';
				else
					$rateImg='images/r3.gif';
			elseif($ratingAvg >= 4 and 	$ratingAvg<=5)
				if($ratingAvg >= 4.5)
					$rateImg='images/r4_half.gif';
				else
					$rateImg='images/r4.gif';
			elseif($ratingAvg >= 5)
				$rateImg='images/r5.gif';
		}
		else
			$rateImg='images/r0.gif';
		return $rateImg;

}

//get file extension
function getFileExtension($str) 
{
	$i = strrpos($str,".");
	if (!$i) { return ""; }

	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return $ext;
}
//select the given value in select box
function selectBox($name, $valueArray, $selected='', $extras='') 
{
	//echo $selected;
	$content = '<select name="' . $name . '" id="' . $name . '" ' . $extras . '>';
	//print_r($valueArray);
	if(is_array($valueArray)) 
		{
			foreach($valueArray as $key=>$value)
				{
					//echo $key."<br>";
					if($key == $selected) 
						{
							//echo $value;
							$isselected='selected';
						} 
					else 
						{
							$isselected='';
						}
					$content .= "\n\t";
					$content .= '<option value="' . $key . '" ' . $isselected . '>' . $value . '</option>';
				}
		}
    $content .= "\n";
    $content .= '</select>';
	
	return $content;
}
function createDay()
{
	$day	= array();
	$day	= array("-1"=>"День","1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9","10"=>"10","11"=>"11","12"=>"12","13"=>"13","14"=>"14","15"=>"15","16"=>"16","17"=>"17","18"=>"18","19"=>"19","20"=>"20","21"=>"21","22"=>"22","23"=>"23","24"=>"24","25"=>"25","26"=>"26","27"=>"27","28"=>"28","29"=>"29","30"=>"30","31"=>"31");
	return $day;
}
function createMon()
{
	$month	= array();
	$month	= array("-1"=>"Місяць","01"=>"Січня","02"=>"Лютого","03"=>"Березня","04"=>"Квітня","05"=>"Травня","06"=>"Червня","07"=>"Липня","08"=>"Серпня","09"=>"Вересня","10"=>"Жовтня","11"=>"Листопада","12"=>"Грудня");
	return $month;
}

function createLimitedYear()
{
	$year	= array();
	$year	= array("-1"=>"Рік","2006"=>"2006","2007"=>"2007","2008"=>"2008","2009"=>"2009");
	return $year;
}

function monArray()
{
	$month	= array();
	$month	= array("Січень"=>"Січень","Лютий"=>"Лютий","Березень"=>"Березень","Квітень"=>"Квітень","Травень"=>"Травень","Червень"=>"Червень","Липень"=>"Липень","Серпень"=>"Серпень","Вересень"=>"Вересень","Жовтень"=>"Жовтень","Листопад"=>"Листопад","Грудень"=>"Грудень");
	return $month;
}
function dayArray()
{
	$day	= array();
	$day	= array("1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9","10"=>"10","11"=>"11","12"=>"12","13"=>"13","14"=>"14","15"=>"15","16"=>"16","17"=>"17","18"=>"18","19"=>"19","20"=>"20","21"=>"21","22"=>"22","23"=>"23","24"=>"24","25"=>"25","26"=>"26","27"=>"27","28"=>"28","29"=>"29","30"=>"30","31"=>"31");
	return $day;
}
function hrsArray()
{
	$hrs	= array();
	$hrs	= array("1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9","10"=>"10","11"=>"11","12"=>"12");
	return $hrs;
}
function minsArray()
{
	$mins	= array();
	$mins	= array("00"=>"00","05"=>"05","10"=>"10","15"=>"15","20"=>"20","25"=>"25","30"=>"30","35"=>"35","40"=>"40","45"=>"45","50"=>"50","55"=>"55");
	return $mins;
}
function ampmArray()
{
	$ampm	= array();
	$ampm	= array("am"=>"am","pm"=>"pm");
	return $ampm;
}

function createMonth()
{
	$month	= array();
	$month	= array(""=>"","01"=>"Січень","02"=>"Лютий","03"=>"Березень","04"=>"Квітень","05"=>"Травень","06"=>"Червень","07"=>"Липень","08"=>"Серпень","09"=>"Вересень","10"=>"Жовтень","11"=>"Листопад","12"=>"Грудень");
	return $month;
}

function createYear()
{
	$year	= array();
	$year	= array("2007"=>"2007","2006"=>"2006","2005"=>"2005","2004"=>"2004","2003"=>"2003","2002"=>"2002","2001"=>"2001","2000"=>"2000","1999"=>"1999","1998"=>"1998","1997"=>"1997","1996"=>"1996","1995"=>"1995","1994"=>"1994","1993"=>"1993","1992"=>"1992","1991"=>"1991","1990"=>"1990","1989"=>"1989","1988"=>"1988","1987"=>"1987","1986"=>"1986","1985"=>"1985","1984"=>"1984","1983"=>"1983","1982"=>"1982","1981"=>"1981","1980"=>"1980","1979"=>"1979","1978"=>"1978","1977"=>"1977","1976"=>"1976","1975"=>"1975","1974"=>"1974","1973"=>"1973","1972"=>"1972","1971"=>"1971","1970"=>"1970","1969"=>"1969","1968"=>"1968","1967"=>"1967","1966"=>"1966","1965"=>"1965","1964"=>"1964","1963"=>"1963","1962"=>"1962","1961"=>"1961","1960"=>"1960","1959"=>"1959","1958"=>"1958","1957"=>"1957","1956"=>"1956","1955"=>"1955","1954"=>"1954","1953"=>"1953","1952"=>"1952","1951"=>"1951","1950"=>"1950","1949"=>"1949","1948"=>"1948","1947"=>"1947","1946"=>"1946","1945"=>"1945","1944"=>"1944","1943"=>"1943","1942"=>"1942","1941"=>"1941",
					"1940"=>"1940","1939"=>"1939","1938"=>"1938","1937"=>"1937","1936"=>"1936","1935"=>"1935","1934"=>"1934","1933"=>"1933","1932"=>"1932","1931"=>"1931","1930"=>"1930","1929"=>"1929","1928"=>"1928","1927"=>"1927","1926"=>"1926","1925"=>"1925","1924"=>"1924","1923"=>"1923","1922"=>"1922","1921"=>"1921","1920"=>"1920","-1"=>"Рік");
	return $year;
}
function createSchoolYear()
{
	$year	= array();
	$year	= array("-1"=>"Оберіть рік: ","2010"=>"2010","2009"=>"2009","2008"=>"2008","2007"=>"2007","2006"=>"2006","2005"=>"2005","2004"=>"2004","2003"=>"2003","2002"=>"2002","2001"=>"2001","2000"=>"2000","1999"=>"1999","1998"=>"1998","1997"=>"1997","1996"=>"1996","1995"=>"1995","1994"=>"1994","1993"=>"1993","1992"=>"1992","1991"=>"1991","1990"=>"1990","1989"=>"1989","1988"=>"1988","1987"=>"1987","1986"=>"1986","1985"=>"1985","1984"=>"1984","1983"=>"1983","1982"=>"1982","1981"=>"1981","1980"=>"1980","1979"=>"1979","1978"=>"1978","1977"=>"1977","1976"=>"1976","1975"=>"1975","1974"=>"1974","1973"=>"1973","1972"=>"1972","1971"=>"1971","1970"=>"1970","1969"=>"1969","1968"=>"1968","1967"=>"1967","1966"=>"1966","1965"=>"1965","1964"=>"1964","1963"=>"1963","1962"=>"1962","1961"=>"1961","1960"=>"1960","1959"=>"1959","1958"=>"1958","1957"=>"1957","1956"=>"1956","1955"=>"1955","1954"=>"1954","1953"=>"1953","1952"=>"1952","1951"=>"1951","1950"=>"1950","1949"=>"1949","1948"=>"1948","1947"=>"1947","1946"=>"1946",
					"1945"=>"1945","1944"=>"1944","1943"=>"1943","1942"=>"1942","1941"=>"1941","1940"=>"1940","1939"=>"1939","1938"=>"1938","1937"=>"1937","1936"=>"1936","1935"=>"1935","1934"=>"1934","1933"=>"1933","1932"=>"1932","1931"=>"1931","1930"=>"1930","1929"=>"1929","1928"=>"1928","1927"=>"1927","1926"=>"1926","1925"=>"1925","1924"=>"1924","1923"=>"1923","1922"=>"1922","1921"=>"1921","1920"=>"1920");
	return $year;
}
function createWorkYear()
{
	$year	= array();
	$year	= array("-1"=>"Рік","2010"=>"2010","2009"=>"2009","2008"=>"2008","2007"=>"2007","2006"=>"2006","2005"=>"2005","2004"=>"2004","2003"=>"2003","2002"=>"2002","2001"=>"2001","2000"=>"2000","1999"=>"1999","1998"=>"1998","1997"=>"1997","1996"=>"1996","1995"=>"1995","1994"=>"1994","1993"=>"1993","1992"=>"1992","1991"=>"1991","1990"=>"1990","1989"=>"1989","1988"=>"1988","1987"=>"1987","1986"=>"1986","1985"=>"1985","1984"=>"1984","1983"=>"1983","1982"=>"1982","1981"=>"1981","1980"=>"1980","1979"=>"1979","1978"=>"1978","1977"=>"1977","1976"=>"1976","1975"=>"1975","1974"=>"1974","1973"=>"1973","1972"=>"1972","1971"=>"1971","1970"=>"1970","1969"=>"1969","1968"=>"1968","1967"=>"1967","1966"=>"1966","1965"=>"1965","1964"=>"1964","1963"=>"1963","1962"=>"1962","1961"=>"1961","1960"=>"1960","1959"=>"1959","1958"=>"1958","1957"=>"1957","1956"=>"1956","1955"=>"1955");
	foreach ($year as $key=>$value)
		if($key<= date("Y"))
			$newYear[$key]	= $value;	
	return $newYear;
}
function createSort()
{
	$sort	= array();
	$sort	= array("top"=>"Top","asc"=>"Asc","desc"=>"Desc");
	return $sort;
}

function RandomName( $nameLength ) 
{
 $NameChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
 $Vouel = 'AEIOU';
 $Name = "";
 for ($index = 1; $index <= $nameLength; $index++) 
 { 
    if ($index % 3 == 0)
    {
      $randomNumber = rand(1,strlen($Vouel));
      $Name .= substr($Vouel,$randomNumber-1,1); 
    }else
      {
        $randomNumber = rand(1,strlen($NameChars));
        $Name .= substr($NameChars,$randomNumber-1,1);
      } 
 }
 return $Name;
}

// ------------ lixlpixel recursive PHP functions -------------
// recursive_remove_directory( directory to delete, empty )
// expects path to directory and optional TRUE / FALSE to empty
 // of course PHP has to have the rights to delete the directory
 // you specify and all files and folders inside the directory
 // ------------------------------------------------------------

 // to use this function to totally remove a directory, write:
// recursive_remove_directory('path/to/directory/to/delete');

 // to use this function to empty a directory, write:
// recursive_remove_directory('path/to/full_directory',TRUE);

 function recursive_remove_directory($directory, $empty=FALSE)
{
    // if the path has a slash at the end we remove it here
    if(substr($directory,-1) == '/')
     {
         $directory = substr($directory,0,-1);
     }
 
    // if the path is not valid or is not a directory ...
    if(!file_exists($directory) || !is_dir($directory))
    {
         // ... we return false and exit the function
         return FALSE;
 
     // ... if the path is not readable
    }elseif(!is_readable($directory))
    {
        // ... we return false and exit the function
         return FALSE;
  
   // ... else if the path is readable
     }else{
  
         // we open the directory
        $handle = opendir($directory);
  
         // and scan through the items inside
         while (FALSE !== ($item = readdir($handle)))
        {
             // if the filepointer is not the current directory
           // or the parent directory
             if($item != '.' && $item != '..')
            {
                 // we build the new path to delete
                $path = $directory.'/'.$item;
 
                // if the new path is a directory
                 if(is_dir($path)) 
                {
                    // we call this function with the new path
                     recursive_remove_directory($path);
 
               // if the new path is a file
                }else{
                    // we remove the file
                    unlink($path);
                }
             }
        }
         // close the directory
         closedir($handle);
 
         // if the option to empty is not set to true
         if($empty == FALSE)
         {
            // try to delete the now empty directory
            if(!rmdir($directory))
            {
                 // return false if not possible
               return FALSE;
             }
        }
        // return success
         return TRUE;
     }
 }
 // ------------------------------------------------------------

//get month value from string month
function extractMonth($month) 
{
	switch (strtoupper($month)) 
		{
			case "JAN": $exMonth=1; break;
			case "FEB": $exMonth=2; break;
			case "MAR": $exMonth=3; break;
			case "APR": $exMonth=4; break;
			case "MAY": $exMonth=5; break;
			case "JUN": $exMonth=6; break;
			case "JUL": $exMonth=7; break;
			case "AUG": $exMonth=8; break;
			case "SEP": $exMonth=9; break;
			case "OCT": $exMonth=10; break;
			case "NOV": $exMonth=11; break;
			case "DEC": $exMonth=12; break;
		}
	return $exMonth;
}

function php2js($dta)
{
	if(is_object($dta))
		$dta = get_object_vars($dta);
	if(is_array($dta))
		{
			foreach($dta AS $k=>$d)
				$dta[$k] = php2js($k) . ":" . php2js($d);
			return '{' . implode(',',$dta) . '}';
		}
	elseif(is_numeric($dta))
		return "$dta";
	elseif(is_string($dta))
		return '"'.str_replace('"','\"',$dta).'"';
	else
		return 'null';
}

//function to replate special variables by corresponding values
function replaceSpVariables($spArray,$content)
{
	foreach ($spArray as $key => $value)
		$content	= str_replace($key,$value,$content);
	return $content;
}
